package com.majun.huaxishe.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.majun.huaxishe.entity.Photos;
import com.majun.huaxishe.entity.dto.PhotosDTO;
import com.majun.huaxishe.entity.vo.PhotosVO;
import com.majun.huaxishe.exception.BaseException;
import com.majun.huaxishe.result.Result;
import com.majun.huaxishe.service.IPhotosService;
import com.majun.huaxishe.utils.HxsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 照片表 前端控制器
 * </p>
 *
 * @author 马骏
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/photos")
@CrossOrigin
public class PhotosController {

    @Autowired
    private IPhotosService photosService;

    @GetMapping("/queryByAlbumId")
    public Result<List<Photos>> queryByAlbumId(@RequestParam("albumId") Integer albumId){
        List<Photos> photosList = photosService.lambdaQuery()
                .eq(Photos::getAlbumId, albumId)
                .orderByDesc(Photos::getWeight)
                .list();
        return Result.success(photosList);
    }

    @GetMapping("/queryPage")
    public Result<Page<PhotosVO>> queryPage(PhotosDTO photosDTO){
        Page<PhotosVO> page = photosService.queryPage(photosDTO);
        return Result.success(page);
    }

    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids){
        if (HxsUtils.isEmpty(ids)){
            throw new BaseException("请选择1条或多条");
        }
        photosService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/queryById")
    public Result<Photos> queryById(Long id){
        Photos photos = photosService.getById(id);
        return Result.success(photos);
    }

    @PostMapping("/updatePhotos")
    public Result<Photos> updatePhotos(@RequestBody Photos photos){
        photosService.updateById(photos);
        return Result.success();
    }
}
